/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication13;

import java.io.File;

/**
 *
 * @author Irina
 */
public class Delete implements ICommandExecutor 
{
    private static ICommandExecutor instance; 
    
    @Override
    public void execute(String InputString)
    {
        String[] strOut=InputString.split(" ");
        if (strOut.length==1)
        {
            System.out.println("Не был введен адрес. Повторите попытку.");
        }else
        {
            String NameFile=strOut[1];
            
            File fl=new File(NameFile);
            if (!fl.exists()) 
            {
               System.out.println("Данный файл не существует.");
            }
            else
            {
                delete(fl);
                if(!fl.exists())
                {
                    System.out.println(fl.getName()+" удален!");
                    System.out.println();
                }else
                {
                    System.out.println("Операция удаления не удалась.");
                    System.out.println();
                }
            }
        }
    }
    
    private void delete(File fl) {
        if (fl.isDirectory()) 
        {
            String[] children = fl.list();
            for (String elem : children) 
            {
                File f = new File(fl,elem);
                delete(f);
            }
        } 
        fl.delete();
    }
        
    public static ICommandExecutor getInstance()
    {
         if(instance==null)
        {
            instance=new Delete();
        }
        return instance;
    }
}
